<template>
  <div>
    <div class="table_card">
      <!-- 上部分 title -->
      <div class="table_card_top">
        <span class="solid_left"></span>
        <div class="table_card_title">充值规则</div>
        <div class="op_right">
          <button class="primary_button" @click="handleAdd">添加规则</button>
        </div>
      </div>

      <div class="table_list">
        <el-table :data="RuleData" stripe style="width: 100%">
          <el-table-column
            prop="itemStoreTypeName"
            label="分类描述"
            width="180"
          ></el-table-column>
          <el-table-column
            prop="confMoneyUp"
            label="充值区间上限"
            width="250px"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.confMoneyUp }}</span>
              <span>{{ MoneyUpType[scope.row.confMoneyUpType] }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="confMoneyDownType"
            label="充值区间下限"
            width="250px"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.confMoneyDown }}</span>
              <span>{{ MoneyDownType[scope.row.confMoneyDownType] }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="confGiveMoney"
            label="赠送金额"
          ></el-table-column>
          <el-table-column label="应用店面">
            <template slot-scope="scope">
              <div>共{{ scope.row.storeCount }}个门店</div>
              <div class="check" @click="handleCheck(scope.row)">点击查看</div>
            </template>
          </el-table-column>
          <el-table-column prop="date" label="操作" width="200px">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="edit(scope.row)"
                >编辑</el-button
              >
              <el-button size="mini" type="text" @click="dele(scope.row)"
                >删除</el-button
              >
              <el-button
                size="mini"
                type="text"
                @click="Takedown(scope.row, 'Y')"
                :disabled="scope.row.confStatus === 'Y' ? true : false"
                v-hasPermi="['store:conf:avl']"
              >
                启用
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="Takedown(scope.row, 'N')"
                :disabled="scope.row.confStatus === 'N' ? true : false"
                v-hasPermi="['store:conf:avl']"
              >
                禁用
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>

      <!-- 列表 点击查看 -->
      <el-dialog
        title="应用门店"
        :visible.sync="CheckStoresdialogVisible"
        width="450px"
      >
        <span>
          <div class="list_Stores">
            <div
              class="list_Stores_item"
              v-for="(item, index) in StoresList"
              :key="index"
            >
              {{ item.name }}
            </div>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="CancleCheckStore">取 消</el-button>
          <el-button type="primary" @click="CheckStore">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 新增规则 -->
      <el-dialog
        :visible.sync="AddrulesdialogVisible"
        width="450px"
        :title="title"
        class="no-dialog-padding"
        :before-close="handleClose"
      >
        <span>
          <div class="AddRules_box">
            <el-form
              ref="RechargeForm"
              :rules="AddRules"
              :model="RechargeForm"
              label-position="top"
            >
              <el-row>
                <el-form-item label="区间下限" prop="confMoneyDownType">
                  <div class="Rules_form_box">
                    <div class="Rules_down_select">
                      <el-select
                        v-model="RechargeForm.confMoneyDownType"
                        placeholder="请选择"
                        class="w-120"
                      >
                        <el-option
                          v-for="item in MoneyDownTypeOptions"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        >
                        </el-option>
                      </el-select>
                    </div>
                    <div class="Rules_down_ipt">
                      <el-input-number
                        placeholder="请输入金额"
                        v-model="RechargeForm.confMoneyDown"
                        :precision="2"
                        :min="0"
                        :max="1000000"
                        :controls="false"
                        class="text-left w-"
                      ></el-input-number>
                    </div>
                  </div>
                </el-form-item>
              </el-row>
              <el-row>
                <el-form-item label="区间上限" prop="confMoneyUp">
                  <div class="Rules_form_box">
                    <div class="Rules_form_ipt">
                      <el-input-number
                        placeholder="请输入金额"
                        v-model="RechargeForm.confMoneyUp"
                        :precision="2"
                        :min="0"
                        :max="1000000"
                        :controls="false"
                        class="text-left"
                      ></el-input-number>
                    </div>
                    <div class="Rules_form_select">
                      <el-select
                        v-model="RechargeForm.confMoneyUpType"
                        placeholder="请选择"
                        class="w-120"
                      >
                        <el-option
                          v-for="item in MoneyUpTypeOptions"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        >
                        </el-option>
                      </el-select>
                    </div>
                  </div>
                </el-form-item>
              </el-row>
              <el-row>
                <el-col>
                  <el-form-item label="赠送金额" prop="confGiveMoney">
                    <el-input-number
                      class="w-100 text-left"
                      v-model="RechargeForm.confGiveMoney"
                      placeholder="请输入金额"
                      :precision="2"
                      :min="0"
                      :max="1000000"
                      :controls="false"
                    ></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <!-- <el-col>
                  <el-form-item label="规则分类" prop="storeNames">
                    <el-select v-model="sort" placeholder="请选择" class="w-100">
                      <el-option
                        v-for="item in Rulesoptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col> -->
                <!-- <el-col>
                  <el-form-item label="应用门店" prop="storeNames">
                    <div @click="clikApplyStore">
                      <el-input
                        class="w-100"
                        v-model="RechargeForm.storeNames"
                        placeholder="请选择应用门店"
                        readonly
                      ></el-input>
                    </div>
                  </el-form-item>
                </el-col> -->
                <el-col>
                  <el-form-item label="项目门店分类" prop="itemStoreTypeId">
                    <div>
                      <el-select
                        class="w-100 selsectbox"
                        v-model="RechargeForm.itemStoreTypeId"
                        placeholder="请选择项目门店分类"
                      >
                        <el-option
                          v-for="item in ProjectStoreSortOptions"
                          :key="item.itemStoreTypeId"
                          :label="item.typeName"
                          :value="item.itemStoreTypeId"
                        >
                        </el-option>
                      </el-select>
                    </div>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button class="cancel_btn" @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="AddRulesSubmit">确 定</el-button>
        </span>
      </el-dialog>

      <Stores ref="stores" @change="changeStores"></Stores>
    </div>
  </div>
</template>
  
<script>
import {
  getRechargeRules,
  getStoresList,
  CreateRechargeRules,
  UpdateRechargeRules,
  getStoresInfo,
  DeleteStores,
  ConfAvl,
} from "@/api/business/parameter";
import Stores from "@/components/Stores/storeModel";
import { getProjectStoreSortlistAll } from "@/api/business/project";
export default {
  name: "EryuoaStoreFrontendtabList",
  components: { Stores },
  data() {
    return {
      AddrulesdialogVisible: false,
      loading: false,
      title: "",
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        confType: "RU", // 配置类型
      },
      RuleData: [], // 规则列表

      Rulesoptions: [
        {
          value: "1",
          label: "通用规则",
        },
        {
          value: "2",
          label: "新店规则",
        },
      ],
      sort: "",
      RechargeForm: {
        confMoneyDownType: "",
        confMoneyDown: "",
        confMoneyUpType: "",
        confMoneyUp: "",
        confGiveMoney: "",
        storeIds: "",
        itemStoreTypeId: "",
        confType: "RU", // 配置类型
        confId: "",
        storeList: [],
      },
      total: 0,
      textarea: "",
      value: "",

      RechargeList: [], // 充值列表

      AddRules: {
        confMoneyDownType: [
          { required: true, message: "请选择", trigger: "blur" },
        ],
        confMoneyUp: [
          { required: true, message: "请输入金额", trigger: "blur" },
        ],
        confGiveMoney: [
          { required: true, message: "请输入金额", trigger: "blur" },
        ],
        itemStoreTypeId: [
          { required: true, message: "请选择", trigger: "blur" },
        ],
      },

      // 上限类型
      MoneyUpType: {
        LE: "<",
        EL: "≤",
      },

      // 下限类型
      MoneyDownType: {
        UP: ">",
        EU: "≥",
      },

      MoneyUpTypeOptions: [
        {
          value: "LE",
          label: "<",
        },
        {
          value: "EL",
          label: "≤",
        },
      ],

      MoneyDownTypeOptions: [
        {
          value: "UP",
          label: ">",
        },
        {
          value: "EU",
          label: "≥",
        },
      ],

      CheckStoresdialogVisible: false, // 项目列表 点击查看门店
      StoresList: [], // 应用门店

      ProjectStoreSortOptions: [],
    };
  },

  created() {
    this.getList();
  },

  mounted() {},

  methods: {
    // 启用禁用
    Takedown(row, confStatus) {
      this.$confirm(
        "是否确认" + (confStatus === "Y" ? "启用" : "禁用") + "?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          ConfAvl({
            confId: row.confId,
          }).then((res) => {
            const { code } = res;
            if (code === 0) {
              this.getList();
              this.$message({
                type: "success",
                message: "操作成功!",
              });
            }
          });
        })
        .catch(() => {});
    },

    // 项目门店分类
    getStoreSortList() {
      this.loading = true;
      getProjectStoreSortlistAll({})
        .then((res) => {
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.ProjectStoreSortOptions = data;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 获取详情
    getStoresInfos(row) {
      let confId = row.confId;
      getStoresInfo({ confId: confId })
        .then((res) => {
          const { code, data } = res;
          if (code === 0) {
            let {
              confId,
              confMoneyDown,
              confMoneyDownType,
              confMoneyUp,
              confMoneyUpType,
              confGiveMoney,
              itemStoreTypeId,
              storeList,
            } = data;

            let names = [];
            let ids = [];

            for (let i = 0; i < storeList.length; i++) {
              let Item = storeList[i];
              names.push(Item.name);
              ids.push(Item.id);
            }

            this.RechargeForm = {
              confMoneyDownType: confMoneyDownType,
              confMoneyDown: confMoneyDown,
              confMoneyUpType: confMoneyUpType,
              confMoneyUp: confMoneyUp,
              confGiveMoney: confGiveMoney,
              storeIds: ids,
              itemStoreTypeId: itemStoreTypeId,
              confType: "RU", // 配置类型
              confId: confId,
              storeList: storeList,
            };
          }
        })
        .catch((err) => {});
    },

    clikApplyStore() {
      this.$nextTick(() => {
        this.$refs.stores.applyStore(this.RechargeForm.storeList);
        this.$refs.stores.getStoresList(); // 调用子组件的方法
      });
    },

    changeStores(res) {
      const { itemStoreTypeId, storeId, list } = res;

      this.RechargeForm.itemStoreTypeId = itemStoreTypeId;
      this.RechargeForm.storeIds = list.map((item) => {
        return item.id;
      });

      this.RechargeForm.storeList = list;

      console.log(this.RechargeForm);
    },

    resQ() {
      this.queryParams.pageNo = 1;
      this.total = 0;
      this.RuleData = [];
      this.getList();
    },

    // 充值规则
    getList() {
      this.loading = true;
      getRechargeRules(this.queryParams)
        .then((res) => {
          console.log(res);
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.RuleData = data.list;
            this.total = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    // 门店列表
    getStores(confId) {
      getStoresList({ confId: confId })
        .then((res) => {
          console.log(res);
          const { code, data } = res;
          if (code === 0) {
            this.StoresList = data;
          }
        })
        .catch((err) => {});
    },

    // 确定
    AddRulesSubmit() {
      this.$refs["RechargeForm"].validate((valid) => {
        if (valid) {
          if (!this.RechargeForm.confId) {
            this.NewRecharge();
          } else {
            this.EditRecharge();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    // 编辑
    EditRecharge() {
      const {
        confId,
        confMoneyDown,
        confMoneyDownType,
        confMoneyUp,
        confMoneyUpType,
        confType,
        confGiveMoney,
        itemStoreTypeId,
        storeIds,
      } = this.RechargeForm;
      UpdateRechargeRules({
        confId: confId,
        confMoneyDown: confMoneyDown,
        confMoneyDownType: confMoneyDownType,
        confMoneyUp: confMoneyUp,
        confMoneyUpType: confMoneyUpType,
        confGiveMoney: confGiveMoney,
        confType: confType,
        itemStoreTypeId: itemStoreTypeId,
        storeIds: storeIds,
      })
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.getList();
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            this.handleClose();
          }
        })
        .catch((err) => {});
    },

    // 新建
    NewRecharge() {
      CreateRechargeRules(this.RechargeForm)
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.getList();
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            this.handleClose();
          }
        })
        .catch((err) => {});
    },

    handleClose() {
      this.resetFormData("RechargeForm");
      this.RechargeForm.confId = "";
      this.$nextTick(() => {
        this.AddrulesdialogVisible = false;
      });
    },

    // 表单重置
    resetFormData(formName) {
      // console.log(this.$refs[formName]);
      this.$refs[formName].resetFields();
    },

    // 项目分类
    projectSortSelect() {
      this.loading = true;
      getProjectSortPage(this.queryParams2)
        .then((res) => {
          console.log(res);
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.SortList = data;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    // 取消 查看门店
    CancleCheckStore() {
      this.CheckStoresdialogVisible = false;
    },

    // 确认 查看门店
    CheckStore() {
      this.CheckStoresdialogVisible = false;
    },

    // 列表点击查看
    handleCheck(row) {
      let confId = row.confId;
      this.CheckStoresdialogVisible = true;
      this.getStores(confId);
    },

    // 筛选
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetFormData("queryForm");
      this.handleQuery();
    },

    // 表单重置
    resetFormData(formName) {
      console.log(this.$refs[formName]);
      this.$refs[formName].resetFields();
    },

    // 列表 删除
    dele(row) {
      this.$confirm("规则删除后无法撤销, 是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          DeleteStores({ id: row.confId }).then((res) => {
            const { code } = res;
            if (code === 0) {
              this.getList();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {});
    },

    handleAdd() {
      this.AddrulesdialogVisible = true;
      this.title = "添加规则";
      this.getStoreSortList();
      this.$nextTick(() => {
        this.RechargeForm = {
          confMoneyDownType: "",
          confMoneyDown: "",
          confMoneyUpType: "",
          confMoneyUp: "",
          confGiveMoney: "",
          storeIds: "",
          itemStoreTypeId: "",
          confType: "RU", // 配置类型
          confId: "",
          storeList: [],
        };
      });
    },

    edit(row) {
      this.AddrulesdialogVisible = true;
      this.title = "编辑规则";
      this.getStoresInfos(row);
      this.getStoreSortList();
    },
  },
};
</script>
  
  <style lang="scss" scoped>
.w-200 {
  width: 200px;
  background: #ffffff;
  border-radius: 4px;
}

.client_name {
  font-size: 14px;
  font-weight: 400;
  color: #0052d9;
}

.give {
  height: 40px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  margin-top: 40px;
  line-height: 40px;
}

.el-form-item {
  margin-bottom: 20px;
}

.recharge_form {
  padding: 10px 24px;
}

.memberName {
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}
.el_row_item {
  margin-bottom: 10px;
}
.el_row_date {
  margin-top: 20px;
}
.el_row_reason {
  margin-top: 10px;
}

.el_row_date {
  .el-form-item {
    margin-bottom: 0px;
  }
}

.separate_left {
  width: 1px;
  height: 14px;
  // background: #e9e9e9;
  margin-right: 12px;
}
.separate_right {
  width: 1px;
  height: 14px;
  // background: #e9e9e9;
  margin-left: 12px;
}

.check {
  font-size: 12px;
  color: #0052d9;
}

.list_Stores {
  height: 250px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.9);
  line-height: 22px;
  .list_Stores_item {
    height: 46px;
    line-height: 46px;
    background: #ffffff;
    padding-left: 47px;
    border-bottom: 1px solid #e7e7e7;
  }
}

.AddRules_box {
  padding: 0 24px;
  .Rules_form_box {
    display: flex;
    .Rules_form_select {
      margin-left: 10px;
    }

    .Rules_down_select {
      margin-right: 10px;
    }
  }
}

.Rules_form_ipt {
  width: 300px;
  .el-input-number {
    width: 100%;
  }
}

.Rules_down_ipt {
  width: 280px;
  .el-input-number {
    width: 100%;
  }
}
</style>